from django.db import models
from django.contrib.auth.models import User


class Course(models.Model):
    """心理成长课程表"""
    COURSE_TYPE_CHOICES = [
        ('basic', '基础课程'),
        ('advanced', '进阶课程'),
        ('special', '专题课程'),
    ]
    
    course_name = models.CharField(
        max_length=200, 
        verbose_name='课程名称',
        db_comment='心理课程名称'
    )
    course_type = models.CharField(
        max_length=50, 
        choices=COURSE_TYPE_CHOICES, 
        verbose_name='课程类型',
        db_comment='课程类型分类'
    )
    description = models.TextField(
        verbose_name='课程描述',
        db_comment='课程详细描述'
    )
    instructor = models.CharField(
        max_length=100, 
        verbose_name='讲师',
        db_comment='课程讲师姓名'
    )
    instructor_intro = models.TextField(
        null=True, 
        blank=True, 
        verbose_name='讲师介绍',
        db_comment='讲师背景和资质介绍'
    )
    cover_image = models.URLField(
        null=True, 
        blank=True, 
        verbose_name='封面图',
        db_comment='课程封面图片URL'
    )
    total_lessons = models.IntegerField(
        verbose_name='课时数',
        db_comment='课程总课时数'
    )
    duration_hours = models.FloatField(
        verbose_name='课程时长',
        db_comment='课程总时长(小时)'
    )
    difficulty_level = models.IntegerField(
        default=1, 
        verbose_name='难度等级',
        db_comment='课程难度等级1-5'
    )
    tags = models.JSONField(
        null=True, 
        blank=True, 
        verbose_name='标签',
        db_comment='课程标签JSON数组'
    )
    is_free = models.BooleanField(
        default=True, 
        verbose_name='是否免费',
        db_comment='课程是否免费'
    )
    price = models.DecimalField(
        max_digits=10, 
        decimal_places=2, 
        null=True, 
        blank=True, 
        verbose_name='价格',
        db_comment='课程价格(元)'
    )
    is_active = models.BooleanField(
        default=True, 
        verbose_name='是否启用',
        db_comment='课程是否启用'
    )
    created_at = models.DateTimeField(
        auto_now_add=True, 
        verbose_name='创建时间',
        db_comment='记录创建时间'
    )
    updated_at = models.DateTimeField(
        auto_now=True, 
        verbose_name='更新时间',
        db_comment='记录更新时间'
    )

    class Meta:
        db_table = 'xinshi_course'
        db_table_comment = '心理成长课程表'
        verbose_name = '心理课程'
        verbose_name_plural = '心理课程'

    def __str__(self):
        return self.course_name


class Lesson(models.Model):
    """课程课时表"""
    course = models.ForeignKey(
        Course, 
        on_delete=models.CASCADE, 
        related_name='lessons',
        verbose_name='所属课程',
        db_comment='关联课程ID'
    )
    lesson_number = models.IntegerField(
        verbose_name='课时序号',
        db_comment='课时在课程中的序号'
    )
    lesson_title = models.CharField(
        max_length=200, 
        verbose_name='课时标题',
        db_comment='课时标题'
    )
    content = models.TextField(
        verbose_name='课时内容',
        db_comment='课时文字内容'
    )
    video_url = models.URLField(
        null=True, 
        blank=True, 
        verbose_name='视频链接',
        db_comment='课时视频URL'
    )
    duration_minutes = models.IntegerField(
        verbose_name='时长',
        db_comment='课时时长(分钟)'
    )
    attachments = models.JSONField(
        null=True, 
        blank=True, 
        verbose_name='附件',
        db_comment='课时附件资源JSON'
    )
    created_at = models.DateTimeField(
        auto_now_add=True, 
        verbose_name='创建时间',
        db_comment='记录创建时间'
    )

    class Meta:
        db_table = 'xinshi_lesson'
        db_table_comment = '课程课时表'
        verbose_name = '课程课时'
        verbose_name_plural = '课程课时'
        ordering = ['lesson_number']

    def __str__(self):
        return f'{self.course.course_name} - 第{self.lesson_number}课'


class CourseEnrollment(models.Model):
    """课程注册记录表"""
    user = models.ForeignKey(
        User, 
        on_delete=models.CASCADE, 
        verbose_name='用户',
        db_comment='关联用户ID'
    )
    course = models.ForeignKey(
        Course, 
        on_delete=models.CASCADE, 
        verbose_name='课程',
        db_comment='关联课程ID'
    )
    enrolled_at = models.DateTimeField(
        auto_now_add=True, 
        verbose_name='注册时间',
        db_comment='用户注册课程时间'
    )
    progress = models.IntegerField(
        default=0, 
        verbose_name='学习进度',
        db_comment='课程完成进度百分比0-100'
    )
    completed_lessons = models.JSONField(
        default=list, 
        verbose_name='已完成课时',
        db_comment='已完成的课时ID数组JSON'
    )
    is_completed = models.BooleanField(
        default=False, 
        verbose_name='是否完成',
        db_comment='课程是否已完成'
    )
    completed_at = models.DateTimeField(
        null=True, 
        blank=True, 
        verbose_name='完成时间',
        db_comment='课程完成时间'
    )
    certificate_url = models.URLField(
        null=True, 
        blank=True, 
        verbose_name='证书链接',
        db_comment='学习证书URL'
    )
    created_at = models.DateTimeField(
        auto_now_add=True, 
        verbose_name='创建时间',
        db_comment='记录创建时间'
    )

    class Meta:
        db_table = 'xinshi_course_enrollment'
        db_table_comment = '课程注册记录表'
        verbose_name = '课程注册'
        verbose_name_plural = '课程注册'
        unique_together = ['user', 'course']

    def __str__(self):
        return f'{self.user.username} - {self.course.course_name}'
